package com.donleo.ssm.service.impl;

import com.donleo.ssm.dao.ISignMapper;
import com.donleo.ssm.dto.UserDto;
import com.donleo.ssm.model.SignModel;
import com.donleo.ssm.model.UserModel;
import com.donleo.ssm.service.ISignService;
import com.donleo.ssm.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liangd
 * date 2020-11-27 10:43
 * code
 */
@Service("signService")
public class SignServiceImpl implements ISignService {

    @Resource
    private ISignMapper signMapper;

    @Override
    public List<UserModel> findAllStudent(UserDto userDto) {
        return signMapper.findAllStudent(userDto);
    }

    @Override
    public Integer getCount(UserDto userDto) {
        return signMapper.getCount(userDto);
    }

    @Override
    public List<UserModel> findTeamMemberByUserId(Integer userId) {
        //当天时间 yyyy-MM-dd 00:00:00
        String currentDay = DateUtils.getCurrentDay();
        //下一天时间 yyyy-MM-dd 00:00:00
        String nextDay = DateUtils.getNextDay();
        List<UserModel> list = new ArrayList<>();
        //查询当天已经签到的组员
        List<UserModel> signedList = signMapper.findSignedStudent(userId, currentDay, nextDay);
        //查询该组的组员
        List<UserModel> stuList = signMapper.findTeamMemberByUserId(userId);
        for (UserModel aStuList : stuList) {
            UserModel userModel = new UserModel();
            userModel.setId(aStuList.getId());
            userModel.setStudentId(aStuList.getStudentId());
            userModel.setUsername(aStuList.getUsername());
            userModel.setAvatar(aStuList.getAvatar());
            userModel.setGender(aStuList.getGender());
            userModel.setPhone(aStuList.getPhone());
            userModel.setTeam(aStuList.getTeam());
            userModel.setUserRole(aStuList.getUserRole());
            list.add(userModel);
            //表示有已经签到的学生
            if (signedList.size() > 0) {
                for (UserModel aSignedList : signedList) {
                    UserModel model = new UserModel();
                    model.setId(aSignedList.getId());
                    //判断学生是否已经签到，已经签到的学生就移除
                    if (model.getId().equals(userModel.getId())) {
                        list.remove(userModel);
                    }
                }
            }
        }

        return list;
    }

    @Override
    public Integer getTeamMemberCount(Integer userId) {
        return signMapper.getTeamMemberCount(userId);
    }

    @Override
    public Integer saveSignData(List<SignModel> list) {

        List<SignModel> signModels = new ArrayList<>();
        for (SignModel aList : list) {
            SignModel signModel = new SignModel();
            signModel.setUserId(aList.getUserId());
            signModel.setSignTime(aList.getSignTime());
            //判断迟到时间是否为空
            if (aList.getLateTime() != null && !"".equals(aList.getLateTime())) {
                signModel.setLateTime(DateUtils.getDate(aList.getLateTime()));
            }
            signModel.setSignStatus(aList.getSignStatus());
            signModels.add(signModel);
        }

        return signMapper.saveSignData(signModels);
    }

    @Override
    public List<UserDto> findAllSignRecord(UserDto userDto) {
        return signMapper.findAllSignRecord(userDto);
    }

    @Override
    public Integer getSignRecordCount(UserDto userDto) {
        return signMapper.getSignRecordCount(userDto);
    }

    @Override
    public Integer delete(Integer id) {
        return signMapper.delete(id);
    }

    @Override
    public Integer deletes(List<Integer> idsList) {
        return signMapper.deletes(idsList);
    }

    @Override
    public SignModel findById(Integer id) {
        return signMapper.findById(id);
    }

    @Override
    public Integer update(SignModel signModel) {
        signModel.setLateTime(DateUtils.getDate(signModel.getLateTime()));
        //如果修改状态不为迟到，则设置迟到时间为空
        if (!"late".equals(signModel.getSignStatus())){
            signModel.setLateTime("");
        }
        return signMapper.update(signModel);
    }
}
